package com.jshare.action;

import java.util.ArrayList;

import java.util.List;

import javax.annotation.Resource;

import com.jshare.entity.Member;
import com.jshare.entity.Tiezi;
import com.jshare.service.TieziService;
import com.jshare.tiezi.use.TieziToUse;
import com.jshare.tiezi.use.fenye.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

public class TieziAction extends ActionSupport {
	// 帖子 相关操作的  service
	@Resource(name="tieziService")
	private TieziService tieziService;
	// 将 页面传过来的参数  封装成 对象
	@Resource(name="tieziToUseService")
	private TieziToUse tiziToUseService; 
	
	
	// 下面是分页用的参数
	private String toppic;
	private String tags;
	private String member_name = "张山";
	private List<Tiezi> allties_list;
	private String curPage;
	private int pageCount;
	private Member member;
	


	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getCurPage() {
		return curPage;
	}

	public void setCurPage(String curPage) {
		this.curPage = curPage;
	}

	public List<Tiezi> getAllties_list() {
		return allties_list;
	}

	public void setAllties_list(List<Tiezi> allties_list) {
		this.allties_list = allties_list;
	}

	public String getMember_name() {
		return member_name;
	}

	public void setMember_name(String member_name) {
		this.member_name = member_name;
	}

	public String getToppic() {
		return toppic;
	}

	public void setToppic(String toppic) {
		this.toppic = toppic;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public TieziToUse getTiziToUseService() {
		return tiziToUseService;
	}

	public void setTiziToUseService(TieziToUse tiziToUseService) {
		this.tiziToUseService = tiziToUseService;
	}

	public TieziService getTieziService() {
		return tieziService;
	}

	public void setTieziService(TieziService tieziService) {
		this.tieziService = tieziService;
	}
	
	
	/*
	 * 添加单个帖子
	 */
	
	public String addTiezi(){
		//toppic = LanTranfer.tranfer(toppic);
		//tags = LanTranfer.tranfer(tags);
		/*
		 *  member_name  在 action 里面指定的时候 不需要 转换 ，否则要出错
		 *  member_name   是从页面传递过来的需要转换
		 */
//		member_name = LanTranfer.tranfer(member_name);
//		System.out.println("menber :"+member_name);
		member_name = member.getName();
		Tiezi tie = tiziToUseService.changetoTiezi(toppic, tags, member_name);
		tieziService.addTiezi(tie);
		return SUCCESS;
	}
	
	/*
	 *   查看所有帖子  ， 以  不分页   的方式查看
	 */
	
	public String getAllTies(){
		this.setAllties_list(tieziService.getAllTies());
		return SUCCESS;
	}
	
	
	/*
	 * 		分页的方式查看所有帖子
	 * 		getAllTiesByFenye 中的第二个参数    是设置  每一页显示的 个数（自己可以设置）
	 */
	
	public String getAllTiesByFenye (){
		String sql = "form Tiezi t";
		if(curPage==null){
			curPage = "1";
		}
		int cur = Integer.parseInt(curPage);
		// size 表示 每一页显示的数目 （自己可以设置)
		int size = 30;
		PageInfo info = tieziService.getAllTiesByFenye(cur, size, sql);
		setPageCount(info.getPageCount());
		setAllties_list(info.getResultlist());
		return SUCCESS;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	
}
